module.exports =(res,urlObj,bodyObj,sql) => {
  const keys={
    num:["hp","ap","pow","mag","ski","spe","def","res","luc","mov"],
    str:["name","dutyType","srcName"],
    obj:[],
  }
  sql.query(getSaveQuery(keys,bodyObj),  (error, results, fields) =>{
    if (error) res.end(JSON.stringify({code:0,msg:"请求失败"}));
    else {
      sql.query('SELECT LAST_INSERT_ID()',(error, results, fields)=>{
        res.end(JSON.stringify({code:0,msg:"请求成功",data:{id:results[0]["LAST_INSERT_ID()"]}}));
      })
    }
  });
}

function getSaveQuery(keys,bodyObj){
  const insertVal=keys.num.map(item=>bodyObj[item]).concat(
    keys.str.map(item=>`'${bodyObj[item]}'`),
    keys.obj.map(item=>`'${JSON.stringify(bodyObj[item])}'`)
  ).join(",");
  
  const updataVal=keys.num.map(item=>`${item}=${bodyObj[item]}`).concat(
    keys.str.map(item=>`${item}='${bodyObj[item]}'`),
    keys.obj.map(item=>`${item}='${JSON.stringify(bodyObj[item])}'`)
  ).join(",");
  const insertStr=`INSERT INTO heros (${keys.num.concat(keys.str,keys.obj).join(",")}) VALUES (${insertVal})`;
  const updateStr=`UPDATE heros SET ${updataVal}  WHERE id =${bodyObj.id}`;
  return bodyObj.id?updateStr:insertStr
}
